package lowijs.XfireClient;

import java.util.ArrayList;

import lowijs.XfireClient.event.*;
import lowijs.XfireClient.util.Util;

public class Friend {
	private String username, nick, statustext = null, gamename = null;
	private byte[] userid, sid, image;
	private int imindex = 1;
	
	private final ArrayList<IM> ims = new ArrayList<IM>();
	
	public Friend(String login) {
		this.username = login;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String login) {
		this.username = login;
	}
		
	public String getNick() {
		return nick;
	}
	
	public void setNick(String nick) {
		this.nick = nick;
	}
	
	public String getStatusText() {
		if (gamename != null)
			return gamename;
		else
			return statustext;
	}
	
	public void setStatusText(String statustext) {
		this.statustext = statustext;
	}
	
	public void setInGame(String gamename, byte[] image) {
		this.gamename = gamename;
		this.image = image;
	}
	
	public void setOutOfGame() {
		gamename = null;
		image = null;
	}
	
	public byte[] getUserid() {
		return userid;
	}
	
	public int getUseridAsInt() {
		return Util.intFromBytes(userid, 0, 4);
	}
	
	public void setUserid(byte[] userid) {
		this.userid = userid;
	}
	
	public byte[] getSid() {
		return sid;
	}
	
	public void setSid(byte[] sid) {
		this.sid = sid;
	}
	
	public byte[] getImage() {
		return image;
	}
	
	public void setOffline() {
		sid = null;
	}
	
	public boolean isOnline() {
		return sid != null;
	}

	public ArrayList<IM> getConversation() {
		return ims;
	}
	
	public void receiveIM(IM im) {
		ims.add(im);
		imindex++;
	}
	
	public void sendIM(String message) {
		if (!isOnline())
			return;
		
		IM im = new IM(null, message);
		ims.add(im);
		imindex++;
		SendImEvent sie = new SendImEvent(message, sid, imindex);
		EventManager.fireEvent(sie);
	}
}
